package com.sky.wsp.sql.scanner.patterns;

import com.sky.wsp.sql.scanner.vo.MatchResult;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
public abstract class BasePattern {
    private Map<String, Pattern> patterns = new HashMap<>();

    public List<MatchResult> match(String content) {
        List<MatchResult> list = new ArrayList<>();
        patterns.entrySet().stream().forEach(entry -> {
            String patternName = entry.getKey();
            Pattern pattern = entry.getValue();
            int i = 0;
            Matcher matcher = pattern.matcher(content);
            while (matcher.find()) {
                ++i;
            }
            if (0 == i) {
                return;
            }
            MatchResult r = new MatchResult();
            r.setKey(patternName);
            r.setCount(i);
            list.add(r);
        });
        return list;
    }
}
